package com.example.transaction.sys.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.transaction.common.utils.JwtUtil;
import com.example.transaction.sys.entity.Classfy;
import com.example.transaction.sys.mapper.ClassfyMapper;
import com.example.transaction.sys.mapper.ItemClassfyMapper;
import com.example.transaction.sys.service.IClassfyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zh
 * @since 2023-06-20
 */
@Service
public class ClassfyServiceImpl extends ServiceImpl<ClassfyMapper,Classfy> implements IClassfyService {

    private final ClassfyMapper classfyMapper ; // 假设使用MyBatis Plus的Mapper接口

    public ClassfyServiceImpl(ClassfyMapper classfyMapper) {
        this.classfyMapper = classfyMapper;
    }


    @Override
    public List<String> listAllClassfyNames() {
        LambdaQueryWrapper<Classfy> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Classfy::getClassfyName).groupBy(Classfy::getClassfyName);
        List<String> classfyNameList = listObjs(wrapper, obj -> (String) obj);
        return classfyNameList;
    }

    @Override
    public List<String> getClassfyNamesByclassfyID(Long classfyID) {
        LambdaQueryWrapper<Classfy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Classfy::getclassfyID, classfyID);
        List<Classfy> classfyList = list(wrapper);

        List<String> classfyNames = new ArrayList<>();
        for (Classfy classfy : classfyList) {
            classfyNames.add(classfy.getClassfyName());
        }
        return classfyNames;
    }

    @Override
    public Integer getclassfyIDByClassfyName(String classfyName) {
        LambdaQueryWrapper<Classfy> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Classfy::getClassfyName, classfyName);
        //List<Classfy> classfyList = list(wrapper);

//        List<Integer> classfyID = new ArrayList<>();
//        for (Classfy classfy : classfyList) {
//            classfyID.add(classfy.getclassfyID());
//        }
        Classfy classfy=classfyMapper.selectOne(wrapper);
        if(classfy!=null)return classfy.getclassfyID();
        return null;

//        return classfyID;
    }

//    @Override
//    public boolean save(Classfy classfy) {
//        try {
//            // 在这里完成保存到数据库的逻辑，例如使用 mapper 执行插入操作
//            baseMapper.insert(classfy);
//
//        } catch (Exception e) {
//            // 如果发生异常，可以选择抛出异常或返回错误信息
//            throw new RuntimeException("保存失败: " + e.getMessage());
//
//        }
//        return true;
//    }

}
